import cv2
import numpy as np
img = cv2.imread('../data/paper.jpg')
# cv2.imshow('img',img)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#二值化
# t,binary = cv2.threshold(img_gray,185,255,cv2.THRESH_BINARY)
# cv2.imshow('binary',binary)
#sobel
# sobel = cv2.Sobel(img_gray,cv2.CV_64F,1,1)
# cv2.imshow('sobel',sobel)

blured = cv2.GaussianBlur(img_gray,(5,5),0)

dilate = cv2.dilate(blured,(3,3))
#canny
canny = cv2.Canny(dilate,30,120)
# canny = cv2.resize(canny,(300,300))
# cv2.imshow('canny',canny)
ret, binary = cv2.threshold(canny, 127, 255, cv2.THRESH_BINARY)

# 查找图像边沿：cv2.findContours
img, contours, hierarchy = cv2.findContours(binary,  # 二值化处理后的图像
                                            cv2.RETR_EXTERNAL,  # 只检测外轮廓
                                            cv2.CHAIN_APPROX_NONE)
# img,cnts,hie = cv2.findContours(canny,cv2.RETR_EXTERNAL,
#                                   cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(img,cnts,-1,(0,0,255))
# cv2.imshow('img_cnt',img)
arr_cnt = np.array(contours)
print(arr_cnt[0].shape)
print(arr_cnt[1].shape)
print(arr_cnt[2].shape)
print(arr_cnt[3].shape)
cv2.waitKey()
cv2.destroyAllWindows()






